package com.ciko.code.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciko.code.bean.MyFieldWorkBean;
import com.mywork.R;
import com.mywork.activity.MyFieldWorkDetailActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by LEECO on 2016/6/2.
 */
public class MyFieldWorkAdapter extends BaseAdapter {

    private List<Object> list ;
    private Context context;
    private int type;//类型，，为1时是我的外勤，为2时是收到的外勤
    private final int SEND=1;//发出的签到
    private final int RECEIVE=2;//收到的签到
    public MyFieldWorkAdapter( Context context,List<Object> list,int type){
        this.list = list ;
        this.context = context;
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder mh = null;
        if (view==null){
            mh = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_myfieldwork,null);
            mh.ll_item_myfieldwork_date = (LinearLayout) view.findViewById(R.id.ll_item_myfieldwork_date);
            mh.ll_item_myfieldwork_reason = (LinearLayout) view.findViewById(R.id.ll_item_myfieldwork_reason);
//            mh.v_item_myfieldwork_reason =  view.findViewById(R.id.v_item_myfieldwork_reason);
            mh.v_item_myfieldwork =  view.findViewById(R.id.v_item_myfieldwork);
            mh.tv_item_myfieldwork_date = (TextView) view.findViewById(R.id.tv_item_myfieldwork_date);
            mh.tv_item_myfieldwork_time = (TextView) view.findViewById(R.id.tv_item_myfieldwork_time);
            mh.tv_item_myfieldwork_reason = (TextView) view.findViewById(R.id.tv_item_myfieldwork_reason);
            mh.tv_item_myfieldwork_name = (TextView) view.findViewById(R.id.tv_item_myfieldwork_name);
            view.setTag(mh);
        }else{
            mh = (ViewHolder) view.getTag();
        }
        if (list.get(i) instanceof String){
            mh.ll_item_myfieldwork_date.setVisibility(View.VISIBLE);
            mh.ll_item_myfieldwork_reason.setVisibility(View.GONE);
//            mh.v_item_myfieldwork_reason.setVisibility(View.GONE);
            mh.v_item_myfieldwork.setVisibility(View.GONE);
            mh.tv_item_myfieldwork_date.setText(list.get(i).toString());
        }else{
            mh.ll_item_myfieldwork_date.setVisibility(View.GONE);
            mh.ll_item_myfieldwork_reason.setVisibility(View.VISIBLE);
            mh.v_item_myfieldwork.setVisibility(View.VISIBLE);
//            mh.v_item_myfieldwork_reason.setVisibility(View.VISIBLE);

            final MyFieldWorkBean myFieldWorkBean = (MyFieldWorkBean) list.get(i);
            mh.tv_item_myfieldwork_time.setText(convertDate(myFieldWorkBean.getCreateTime()).split(" ")[1]);
            mh.tv_item_myfieldwork_reason.setText("拜访"+myFieldWorkBean.getCustName());
            if (type==SEND){
                mh.tv_item_myfieldwork_name.setVisibility(View.GONE);
            }else if(type==RECEIVE){
                mh.tv_item_myfieldwork_name.setVisibility(View.VISIBLE);
                mh.tv_item_myfieldwork_name.setText(myFieldWorkBean.getStaffName());
            }
            mh.ll_item_myfieldwork_reason.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context,MyFieldWorkDetailActivity.class);
                    intent.putExtra("type",type);
                    intent.putExtra("bean",myFieldWorkBean);
                    context.startActivity(intent);
                }
            });
        }
        mh.ll_item_myfieldwork_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return view;
    }


    private class ViewHolder{
        private LinearLayout ll_item_myfieldwork_date,ll_item_myfieldwork_reason;
        private View v_item_myfieldwork_reason,v_item_myfieldwork;
        private TextView tv_item_myfieldwork_date,tv_item_myfieldwork_time,
                tv_item_myfieldwork_reason,tv_item_myfieldwork_name;
    }
    public String convertDate(long timeT){
        SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = df3.format(new Date( timeT));
        return date ;
    }
}
